package com.qyhl.webtv.module_microvideo.shortvideo.play.comment;


import com.google.gson.Gson;
import com.qyhl.webtv.basiclib.utils.StringUtils;
import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.basiclib.utils.network.model.ApiResult;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.entity.microvideo.ShortVideoNewCommentBean;
import com.qyhl.webtv.module_microvideo.common.VideoUrl;

public class ShortVideoCommentModel implements ShortVideoCommentContract.ShortVideoCommentModel {
    private ShortVideoCommentPresenter mPresenter;

    ShortVideoCommentModel(ShortVideoCommentPresenter mPresenter) {
        this.mPresenter = mPresenter;
    }

    @Override
    public void getComment(String workId, final String page) {

        EasyHttp.get(VideoUrl.GET_SHORT_VIDEO_COMMENT_LIST)
                .params("workId", workId)
                .params("page", page)
                .execute(new SimpleCallBack<ShortVideoNewCommentBean>() {
                    @Override
                    public void onError(ApiException e) {
                        if (e.getCode() == 202) {
                            mPresenter.setCommentError("暂无任何评论！", !page.equals("1"));
                        } else {
                            mPresenter.setCommentError("获取列表失败，请重新获取！", !page.equals("1"));
                        }
                    }

                    @Override
                    public void onSuccess(ShortVideoNewCommentBean shortVideoNewCommentBean) {
                        if (shortVideoNewCommentBean != null && shortVideoNewCommentBean.getCommentList() != null && shortVideoNewCommentBean.getCommentList().size() > 0) {
                            mPresenter.setComment(shortVideoNewCommentBean, !page.equals("1"));
                        } else {
                            mPresenter.setCommentError("暂无任何评论！", !page.equals("1"));
                        }
                    }
                });
    }

    @Override
    public void addComment(String workId, String content, String userPhone) {

        EasyHttp.post(VideoUrl.GET_SHORT_VIDEO_COMMENT_ADD)
                .params("workId", workId)
                .params("content", content)
                .params("userPhone", userPhone)
                .params("siteId", CommonUtils.getInstance().getSite() + "")
                .execute(new SimpleCallBack<String>() {

                    @Override
                    public void onError(ApiException e) {
                        mPresenter.commentError("评论失败！");
                    }

                    @Override
                    public void onSuccess(String s) {
                        ApiResult resule = new Gson().fromJson(s, ApiResult.class);
                        if (resule.getCode() == 200) {
                            String str = resule.getData().toString();
                            mPresenter.commentSuccess(StringUtils.isNotEmpty(str) ? str.substring(0, str.length() - 2) : "0", resule.getMessage());
                        } else {
                            mPresenter.commentError("评论失败！");
                        }
                    }
                });

    }
}
